Containers - how to work with them
Docker - Overview and how to use it
5 min read
Published Jul 13 2025, updated Aug 17 2026
Guide Sections
Guide Comments
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:latestor evenimageId). - COMMAND – Optional command to override the default one defined in the image
- ARG... – Arguments passed to the command
Basic execution options:
Option | Description | Example |
| Assign a custom name to the container |
|
| Detached mode (run in background) |
|
| Interactive terminal (for shell access) |
|
| Automatically remove the container when it exits |
|
| Attach to container streams |
|
| Override the default entrypoint |
|
Networking options:
Option | Description | Example |
| Publish container's port to host |
|
| Expose a port (used for linking, no host mapping) |
|
| Connect to a custom network |
|
| Set container hostname |
|
Filesystem & volume options:
Options | Description | Example |
| Mount host dir/file into container |
|
| More explicit volume mount syntax (type=volume, bind, tmpfs) |
|
| Run container with a read-only root filesystem |
|
| Mount a tmpfs volume |
|
Resource Limit options:
Options | Description | Example |
| Limit container memory |
|
| Limit number of CPUs |
|
| Set relative CPU weight (default 1024) |
|
| Total memory (memory + swap), set to -1 for unlimited swap |
|
Environment variable options:
Options | Description | Example |
| Set environment variable |
|
| Read env vars from a file |
|
Logging & output options:
Options | Description | Example |
| Specify logging driver (json-file, syslog, none, etc.) |
|
| Set logging options |
|
Security options:
Options | Description | Example |
| Run container as specific user |
|
| Add Linux capabilities |
|
| Set security options (like seccomp profiles) |
|
Example run commands:
docker run -d -p 80:80 --name webserver nginxRuns Nginx webserver in the background, exposing port 80, calling it webserver.
docker run -it --rm ubuntu bashRun an interactive Ubuntu shell command line.
docker run -d -m 512m --cpus="1.5" myappRun 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 myappRun myapp image, passing 3 different ENV variables.
Stopping, starting and pausing containers
docker start mycontainerWill restart a stopped container called mycontainer, you can also reference it by containerId rather than name too.
docker stop mycontainerStops the container named myconatiner, allowing the container to stop gracefully, you can reference it by containerId rather than name too.
docker restart mycontainerTells a running container called mycontainer to restart.
docker pause mycontainerTemporarily suspends processes in a container.
docker unpause mycontainerTemporarily resumes processes in a container.
docker kill mycontainerForcibly 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 mycontainerRemoves a stopped container.
docker rm -f runningcontainerUses the force option to stop and remove running containers.
docker container pruneRemoves all stopped containers.
docker rm $(docker ps -aq)Remove all containers.
Listing containers
docker psLists all running containers.
docker ps -aUses the all option to list all running, stopped, exited etc. containers.
docker container ls --filter status=exitedSame as docker ps. Supports --filter, --format, --quiet (-q).
Container logs
docker logs mycontainerDisplays the logs from a containers stdout/stderr.
docker logs -f mycontainerFollows the logs from a containers stdout/stderr, so keeps outputting new logs.
Inspecting & monitoring containers
docker inspect mycontainerDisplay 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_containeror
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 statsReal-time metrics (CPU, memory, I/O) for containers.
docker top mycontainerShows processes running inside a container.
docker eventsStreams real-time events from Docker daemon (useful for monitoring).
Interacting with containers
docker exec -it mycontainer bashRuns 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 mycontainerAttaches 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.txtCopies a file from the container to the host machine.
docker cp ./config.json mycontainer:/app/config.jsonCopies a file from the host machine to the container.
Updating containers
docker rename oldname newnameRenames a container.
docker update --restart=always my_containerChange the restart policy of a container (e.g., no, on-failure, always, unless-stopped).
docker update --memory 500m mycontainerDynamically 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.--memoryor-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.