Docker
Introduction
Docker is one of the most common software platforms used to create and run containers.
Do Not Use Docker If
- You need a speed boost
- If you prioritize security over convenience
- If you develop desktop GUI applications
- If you want to facilitate development
- If you need to use different OSs or kernels
- If you have a lot of valuable data to store
- If you are looking for an easily managed technology
Commands
Container Management
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)*- * send
SIGTERM
signal to main process +SIGKILL
after 10 seconds
- * send
docker 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
Container Inspection
docker ps
- List running containersdocker ps -a
- List all containersdocker logs [ -f ] container
- Get container logs (stdout
andstderr
)-f
- follow logs
docker 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)
Interacting with Container
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- Useful for debugging
docker wait container
- Wait until the container terminates- Returns exit code
docker commit container image
- Commit a new docker image- Asa a snapshot of the container
Image Management
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 labeltag
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 image
Image Transfer
docker 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 daemons
Builder
FROM image|scratch
- Base image to build onMAINTAINER email
- Author of the imageCOPY path dst
- Copy files from host to imageADD src dst
- Same asCOPY
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 variable
Docker-Compose
The 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.
Example
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
.
References
Web Links
- What is a Container (from docker.com)
- What is Docker? (from docker.com)
- Docker Desktop (from docker.com)
- Docker Hub (from docker.com)
- Use Cases (from docker.com)
- Pros & Cons of Containerization (from AtomicObject)
- Containers: Pros & cons you may not know about (from InfoWorld by David Linthicum)
- Docker Cheatsheet