Build
Build an image from the Dockerfile in the current directory and tag the image
docker build -t myimage -f Dockerfile .
List all images that are locally stored with the Docker Engine
docker ps -a
Delete an image from the local image store
docker image rm CONTAINER_NAME
You may want to remove all orphaned images, you can do this with dandling field
docker rmi $(docker images -f dangling=true -q)
Run
Run a container from the myimage
docker run -it --rm myimage
Stop a running container
docker stop myimage
Kill a running container
docker container kill web
List the running containers (add –all to include stopped containers)
docker container ls
Delete all running and stopped container
docker container rm -f $(docker ps -aq)
Print the last 100 lines of a container’s logs
docker container logs --tail 100 web
Containers
Your basic isolated Docker process. Containers are to Virtual Machines as threads are to processes. Or you can think of them as chroots on steroids.
Lifecycle
docker create
creates a container but does not start it.docker rename
allows the container to be renamed.docker run
creates and starts a container in one operation.docker rm
deletes a container.docker update
updates a container’s resource limits.
Normally if you run a container without options it will start and stop immediately, if you won’t keep it running you can use the command, docker run -td container_id
this will use the option -t
that will allocate a pseudo-TTY session and -d
that will detach automatically the container (run container in background and print container ID).
If you want a transient container, docker run --rm
will remove the container after it stops.
If you want to map a directory on the host to a docker container, docker run -v $HOSTDIR:$DOCKERDIR
. Also, see Volumes.
If you want to remove also the volumes associated with the container, the deletion of the container must include the -v
switch like in docker rm -v
.
There’s also a logging driver available for individual containers in docker 1.10. To run docker with a custom log driver (i.e., to syslog), use docker run --log-driver=syslog
.
Another useful option is docker run --name yourname docker_image
because when you specify the --name
inside the run command, this will allow you to start and stop a container by calling it with the name you specified when you created it.
Starting and Stopping
docker start
starts a container so it is running.docker stop
stops a running container.docker restart
stops and starts a container.docker pause
pauses a running container, “freezing” it in place.docker unpause
will unpause a running container.docker wait
blocks until running container stops.docker kill
sends a SIGKILL to a running container.docker attach
will connect to a running container.
If you want to detach from a running container, use Ctrl + p, Ctrl + q
. If you want to integrate a container with a host process manager, start the daemon with -r=false
then use docker start -a
.
If you want to expose container ports through the host, see the exposing ports section.
Simple working example can be found here:
https://docs.microsoft.com/en-us/dotnet/core/docker/build-container
Docker Cheat Sheet
https://github.com/wsargent/docker-cheat-sheet