Docker is one of the most common software platforms used to create and run containers.
docker create image
- Create container from imagedocker run image
- Create container from image = create
+ start
docker start container
- Start containerdocker stop container
- Stop container (gracefully)*
SIGTERM
signal to main process + SIGKILL
after 10 secondsdocker kill container
- Kill (SIGKILL
) container (forcefully)docker restart container
- = stop
+ start
docker pause container
- Suspend the containerdocker unpause container
- Resume the containerdocker rm [ -f ] container
- Remove container
-f
- force remove running container = docker kill
+ docker rm
docker ps
- List running containersdocker ps -a
- List all containersdocker logs [ -f ] container
- Get container logs (stdout
and stderr
)
-f
- follow logsdocker top container [ps options]
- List processes running inside containerdocker diff container
- Show the differences with the image (modified files)docker inspect container
- Show low-level infos (in JSON format)docker attach container
- Attach to running container (stdin
, stdout
, stderr
)docker cp container:path hostpath
- Copy files/folders from container to hostdocker cp hostpath container:path
- Copy files/folders from host to containerdocker export container
- Export container contents as a tar archivedocker exec container args
- Run command in existing container
docker wait container
- Wait until the container terminates
docker commit container image
- Commit a new docker image
docker images
- List all local imagesdocker history image
- Show the image history (list of ancestors or layers)docker inspect image
- Show low-level infos (in JSON format)docker tag image tag
- Tag image with a label tag
docker commit container image
- Create an image (from a container)docker import url [tag]
- Create an image (from a tar archive)docker rmi image
- Remove imagedocker pull image[:tag]
- Pull image from registrydocker push image[:tag]
- Push image to registrydocker search text
- Search an image on registrydocker login
- Login to registrydocker logout
- Logout from registrydocker save repo[:tag]
- Export image/repo as tar archivedocker load
- Import image from tar archivedocker-ssh image
- Script to transfer images via SSH between two daemonsFROM image|scratch
- Base image to build onMAINTAINER email
- Author of the imageCOPY path dst
- Copy files from host to imageADD src dst
- Same as COPY
but un-tar archives and accept HTTP urlsUSER name
- set the default usernameWORKDIR path
- set the default working directoryCMD args
- set the default commandENV name value
- set an environment variableThe docker-compose
utility is used to manage multiple containers together.
Very often we need to run multiple containers together to make an application work.
And they need very specific configurations to work together,
especially in terms of file systems and networking.
The docker-compose.yml
file is used to configure the containers.
As the .yml
extension suggests, it is written in YAML format.
All services
are defined in the docker-compose.yml
file as named dictionaries.
Then each keyed service can have many different kinds of docker configurations.
image
, is the only required configuration and it's the docker image of that service.
Other common configuration keys are build
, command
, environment
, ports
and more.
version: '3.4'
services:
expressrestaurantswagger:
image: jrwtango/expressrestaurantswagger
build:
context: .
dockerfile: ./Dockerfile
environment:
NODE_ENV: development
ports:
- "3000:3000"
This starts a container from the jrwtango/expressrestaurantswagger
image.
The build
configuration is used to build the image from
the Dockerfile
in the current directory.
To specify environment
variables, we use the environment
configuration.
The NODE_ENV
variable is set to development
to specify the environment for node.
Finally ports
is used to map the container's port 3000
to the host's port 3000
.
When running docker-compose up
, the container will be started and the application
will be available at http://localhost:3000
.