Containers - how to work with them

Docker - Overview and how to use it

5 min read

Published Jul 13 2025


12
0
0
0

CLIContainersDevOpsDockerImagesNetworksVolumes

Creating a container

docker create --name mycontainer <image>

Creates a container based of the specified image (either name:tag or image id), names it mycontainer but doesn't start the container running, it creates it in stopped status.



Running a container

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • OPTIONS - See below.
  • IMAGE – The image to use (e.g., nginx, ubuntu, myapp:latest or even imageId).
  • COMMAND – Optional command to override the default one defined in the image
  • ARG... – Arguments passed to the command

Basic execution options:

Option

Description

Example

--name

Assign a custom name to the container

--name webapp

-d

Detached mode (run in background)

-d

-it

Interactive terminal (for shell access)

-it

--rm

Automatically remove the container when it exits

--rm

-a stdin,stdout,stderr

Attach to container streams

-a stdout

--entrypoint

Override the default entrypoint

--entrypoint /bin/bash


Networking options:

Option

Description

Example

-p HOST:CONTAINER

Publish container's port to host

-p 8080:80

--expose

Expose a port (used for linking, no host mapping)

--expose 3306

--network

Connect to a custom network

--network mynet

--hostname

Set container hostname

--hostname devbox



Filesystem & volume options:

Options

Description

Example

-v HOST:CONTAINER

Mount host dir/file into container

-v ./data:/app/data

--mount

More explicit volume mount syntax (type=volume, bind, tmpfs)

--mount type=bind,source=/host,destination=/data

--read-only

Run container with a read-only root filesystem

--read-only

--tmpfs

Mount a tmpfs volume

--tmpfs /run


Resource Limit options:

Options

Description

Example

--memory, -m

Limit container memory

-m 512m

--cpus

Limit number of CPUs

--cpus 1.5

--cpu-shares

Set relative CPU weight (default 1024)

--cpu-shares 512

--memory-swap

Total memory (memory + swap), set to -1 for unlimited swap

--memory-swap 1g


Environment variable options:

Options

Description

Example

-e

Set environment variable

-e NODE_ENV=production

--env-file

Read env vars from a file

--env-file .env


Logging & output options:

Options

Description

Example

--log-driver

Specify logging driver (json-file, syslog, none, etc.)

--log-driver syslog

--log-opt

Set logging options

--log-opt max-size=10m


Security options:

Options

Description

Example

--user

Run container as specific user

--user 1000:1000

--cap-add

Add Linux capabilities

--cap-add NET_ADMIN

--security-opt

Set security options (like seccomp profiles)

--security-opt no-new-privileges




Example run commands:

docker run -d -p 80:80 --name webserver nginx

Runs Nginx webserver in the background, exposing port 80, calling it webserver.


docker run -it --rm ubuntu bash

Run an interactive Ubuntu shell command line.


docker run -d -m 512m --cpus="1.5" myapp

Run an image called myapp, in the background, restricted to 1.5 cpus and 512MB ram.


docker run -e VAR1=value1 -e VAR2=value2 -e VAR3=value3 myapp

Run myapp image, passing 3 different ENV variables.




Stopping, starting and pausing containers

docker start mycontainer

Will restart a stopped container called mycontainer, you can also reference it by containerId rather than name too.


docker stop mycontainer

Stops the container named myconatiner, allowing the container to stop gracefully, you can reference it by containerId rather than name too.


docker restart mycontainer

Tells a running container called mycontainer to restart.


docker pause mycontainer

Temporarily suspends processes in a container.


docker unpause mycontainer

Temporarily resumes processes in a container.


docker kill mycontainer

Forcibly stops a running container by sending SIGKILL It does not gracefully stop running.


docker stop $(docker ps -q)

Stop all running containers.




Removing containers

docker rm mycontainer

Removes a stopped container.


docker rm -f runningcontainer

Uses the force option to stop and remove running containers.


docker container prune

Removes all stopped containers.


docker rm $(docker ps -aq)

Remove all containers.




Listing containers

docker ps

Lists all running containers.


docker ps -a

Uses the all option to list all running, stopped, exited etc. containers.


docker container ls --filter status=exited

Same as docker ps. Supports --filter, --format, --quiet (-q).




Container logs

docker logs mycontainer

Displays the logs from a containers stdout/stderr.


docker logs -f mycontainer

Follows the logs from a containers stdout/stderr, so keeps outputting new logs.




Inspecting & monitoring containers

docker inspect mycontainer

Display detailed json about a containers metadata.


Just like inspecting images, you can use the --format with the Go template syntax, or pipe the result to the jp shell application to format the output or select specific fields.

eg.

docker inspect --format='{{ .Config.Image }}' my_container

or

docker inspect my_container | jq '.[0] | {ID: .Id, Name: .Name}'

Some key fields that are useful in the containers data:

  • .Id - Container ID.
  • .Name - Container name (prefixed with /).
  • .Config.Image - Image used to create container.
  • .Config.Env - List of environment variables.
  • .State.Running - Boolean status of container.
  • .State.ExitCode - Exit code from last run.
  • .NetworkSettings.Ports - Mapped and exposed ports.
  • .Mounts - Volume and bind mounts.
  • .HostConfig - Host-level config (resources, binds, etc.).
  • .NetworkSettings.IPAddress - (Legacy) IP address in bridge mode.

docker stats

Real-time metrics (CPU, memory, I/O) for containers.


docker top mycontainer

Shows processes running inside a container.


docker events

Streams real-time events from Docker daemon (useful for monitoring).




Interacting with containers

docker exec -it mycontainer bash

Runs a command inside a container, in this case bash to give command line access, in some environments you may need to use sh instead of bash, depending on the command line used. The -it stands for interactive mode and can be used to run any application or script inside the container.


docker attach mycontainer

Attaches to a running container’s main process. (Useful for foreground processes; use CTRL+P CTRL+Q to detach without stopping).


docker cp mycontainer:/data/output.txt ./output.txt

Copies a file from the container to the host machine.


docker cp ./config.json mycontainer:/app/config.json

Copies a file from the host machine to the container.




Updating containers

docker rename oldname newname

Renames a container.


docker update --restart=always my_container

Change the restart policy of a container (e.g., no, on-failure, always, unless-stopped).


docker update --memory 500m mycontainer

Dynamically updates resource limits on a container (e.g. memory, CPU).

Key options:

  • --cpu-shares - set CPU share weighting (relative CPU allocation) eg. --cpu-shares 512.
  • --cpus - limit the number of CPUs the container can use, eg. --cpus=1.5.
  • --memory or -m - limit the memory usage eg. --memory=512m.
  • --memory-swap - set the total memory swap usage eg. --memory-swap=1g.
  • --pids-limit - limit the number of processes the container can spawn eg. --pids-limit=100.

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact