13/01/2018 - DOCKER
List below contains mostly used Docker commands. You can see wide range of command here.
Keeps container up and running.
CMD tail -f /dev/null
Obtaining the IP address of the host OS where docker containers run and access to.
$ ip addr show docker0 | grep -Po 'inet \K[\d.]+'
Removes unused data such as images, containers, network, cache so on.
$ docker system prune --volumes
List all images.
$ docker images
Remove all images.
$ docker rmi $(docker images -q)
Remove single image.
$ docker rmi {image-id}
$ docker rmi {image-name}
Creates new image from Dockerfile whose path is relative to where the command is run.
$ docker build -t {new-image-name} .
Show image commit history.
$ docker history {image-name}
$ docker history {image-name:image-tag}
Creating a new image from existing container. New image contains all the changes done in container.
$ docker commit {existing-container-name} {existing-image-name:new-image-tag}
Creating a new image by tagging an existing image.
$ docker tag {existing-image-{id/name/name:existing-tag-id}} {new-image-name:new-tag-id}}
List only active containers.
$ docker ps
$ docker container ls
List active and non-active containers.
$ docker ps -a
$ docker ps --all
$ docker container ls --all
Start exited/stopped container.
$ docker start {container-name/id}
Stop up/running container.
$ docker stop {container-name/id}
Login to up/running container shell.
$ docker attach {container-id}
$ docker attach {container-name}
$ docker exec -it {container-id} /bin/bash
$ docker exec -it {container-name} /bin/bash
Stop all up/running containers.
$ docker stop $(docker ps -a -q)
Remove single container.
$ docker rm {container-id}
$ docker rm {container-name}
Copies local directory into container. This is mainly for production to isolate application code.
$ docker cp {local_dir} {container_name/id}:{container_dir}
Remove all containers.
$ docker rm $(docker ps -a -q)
Create a new container from an exiting image. The /bin/bash
is optional.
$ docker run -i -t -d --name {new-container-name} {existing-image-id}
$ docker run -i -t -d --name {new-container-name} {existing-image-name}
$ docker run -i -t -d --name {new-container-name} {existing-image-id} /bin/bash
$ docker run -i -t -d --name {new-container-name} {existing-image-name} /bin/bash
$ docker run -i -t -d --name {new-container-name} {existing-image-name:existing-image-tag}
$ docker run -i -t -d --name {new-container-name} {existing-image-name:existing-image-tag} /bin/bash
Creating and accessing guest container server from host OS.
$ docker run -d -p 5000:80 --name {new-container-name} {existing-image-name/id}
$ curl 127.0.0.1:5000
Run native linux commands against running containers.
$ docker exec -it {container-name} {linux-command}
Sharing container folder with host OS.
$ docker run -i -t -d -v {host-folder-path}:{container-folder-path} --name {container-name} {image-name/id}
Container details.
$ docker inspect {container-name/id}
Container log list.
$ docker logs -t {container-name/id}
Container log tailing.
$ docker logs -t -f {container-name/id}
List all networks.
$ docker network ls
Remove a network.
$ docker network rm {network-name/id}
Create a new network based on existing "bridge" driver.
$ docker network create --driver bridge {network-name}
Create a new network with specific IP based on existing "bridge" driver. The "subnet-mask" of 24 gives you 255.255.255.0
subnet mask wherase 8 would give you 255.0.0.0
subnet mask.
$ docker network create --subnet {ip-address}/{subnet-mask} --driver bridge {network-name}
Obtaining the IP of given network bridge called api_default
.
$ echo $(docker network inspect api_default | grep Gateway | grep -o -E '[0-9\.]+')
Listing all volumes.
$ docker volume ls
Deleting all unused volumes.
$ docker volume rm $(docker volume ls -q)
See output of docker commands.
$ docker events&
Stopping events debugging.
$ ps aux | grep docker events
$ kill -9 {docker-events-pid}
Container logs.
$ docker logs -t {container-name/id}
Work on image without creating container. Once you are in image, run commands you have in Dockerfile to see errors, logs etc. The best way to debug commands running.
$ docker run -i -t {image-name/id} /bin/bash
Descriptions apply to services which are defined in docker-compose.yml file.
Show mapped details of "docker-compose.yml" file.
$ docker-compose config
Builds all images and runs containers in verbose mode.
$ docker-compose up
Builds all images and runs containers as daemon.
$ docker-compose up -d
Stops all running container.
$ docker-compose stop
Removes all images, container and network.
$ docker-compose down
Removes all images, container, network and volumes.
$ docker-compose down --volumes
Creates single container based on given service name and prints all ENV
variables.
$ docker-compose run {service_name} env
Rebuilds all service images.
$ docker-compose build
Rebuilds all images defined in compose file and restart containers whose images have changed. Used after deployment.
$ docker-compose up -d --build
Rebuilds single image and runs its container. It ignores all the other services including dependent ones.
$ docker-compose up -d --no-deps --build {service_name}
Rebuilds single service image. Often used right before docker-compose up --no-deps -d {service_name}
as in two processes.
$ docker-compose build {service_name}
Brings up a single service container. It ignores all the other services including dependent ones. Often used right after docker-compose build {service_name}
as in two processes.
$ docker-compose up --no-deps -d {service_name}
Builds all docker containers by specifying the docker-compose.yml
file path.
$ docker-compose -f path/to/docker-compose.yml up -d
Runs a one-time command against a service defined in docker-compose.yml
file.
$ docker-compose -f path/to/docker-compose.yml run {service_name} {command_to_run}
Rebuild service images and containers. Often used for deployment process.
$ docker-compose build
$ docker-compose up -d
Restarts all stopped and running services defined in "docker-compose.yml" file but ignores the changes made in "docker-compose.yml" file.
$ docker-compose restart