Containers - how to work with them
Docker - Overview and how to use it
5 min read
Published Jul 13 2025
Guide Sections
Guide Comments
Creating a container
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
- OPTIONS - See below.
- IMAGE – The image to use (e.g.,
nginx
,ubuntu
,myapp:latest
or 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:
Runs Nginx webserver in the background, exposing port 80, calling it webserver.
Run an interactive Ubuntu shell command line.
Run an image called myapp, in the background, restricted to 1.5 cpus and 512MB ram.
Run myapp image, passing 3 different ENV variables.
Stopping, starting and pausing containers
Will restart a stopped container called mycontainer, you can also reference it by containerId rather than name too.
Stops the container named myconatiner, allowing the container to stop gracefully, you can reference it by containerId rather than name too.
Tells a running container called mycontainer to restart.
Temporarily suspends processes in a container.
Temporarily resumes processes in a container.
Forcibly stops a running container by sending SIGKILL It does not gracefully stop running.
Stop all running containers.
Removing containers
Removes a stopped container.
Uses the force
option to stop and remove running containers.
Removes all stopped containers.
Remove all containers.
Listing containers
Lists all running containers.
Uses the all
option to list all running, stopped, exited etc. containers.
Same as docker ps
. Supports --filter
, --format
, --quiet
(-q
).
Container logs
Displays the logs from a containers stdout/stderr.
Follows the logs from a containers stdout/stderr, so keeps outputting new logs.
Inspecting & monitoring containers
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.
or
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.
Real-time metrics (CPU, memory, I/O) for containers.
Shows processes running inside a container.
Streams real-time events from Docker daemon (useful for monitoring).
Interacting with containers
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.
Attaches to a running container’s main process. (Useful for foreground processes; use CTRL+P CTRL+Q
to detach without stopping).
Copies a file from the container to the host machine.
Copies a file from the host machine to the container.
Updating containers
Renames a container.
Change the restart policy of a container (e.g., no
, on-failure
, always
, unless-stopped
).
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
.