Docker : Frequently Used Commands

Collection of frequently used docker commands


                

Command

Syntax

Example

Notes

Create Docker Container

Manually create a docker container

docker create <image name>

docker create hello-world

 

 

Start a created/stopped Container

Manually start a created / stopped docker container

docker start  -a <container id>

 

-a : watches the output from the container and prints on the console

Check the output logs during the manual start of the container

docker logs <containerid>

 

If -a is not specified in the start container command, the output will not be printed during start of the container. Logs command can be used to check the output after the container is started

Run Docker Containers

Run a image in a docker container

docker run <image name>

docker run hello-world

docker run =  docker create + docker start

Run a image in a docker container and keep the container running in the background

docker run -d <image name>

docker run -d hello-world

 

Run image in a container and execute a command in the started container

docker run <image name> <command>

docker run busybox echo hello

 

docker run busybox ls

 

 

Port forwarding the requests from local machine to the container

docker run -p <port number of localhost>:<port number of the container> <image name>

docker run -p 8080:8080 princearoraaws/simpleweb

 

List docker containers and resources

List the running docker containers

docker ps

 

docker container ls

 

 

List all the docker containers

docker ps –a

 

docker container ls -a

 

-a: Lists all the containers

List all container ids only

docker container ls -aq

docker ps –aq

 

 

-q: List only container ids

 

-a: List all the containers

List docker volumes

docker volume ls

 

 

Stop docker containers

Stop all the running containers

docker container stop $(docker container ls -aq)

 

 

Stop the docker container

docker stop <container id>

 

 

Kil the docker container instance

docker kill <container id>

 

 

Prune / Remove docker containers and resources

Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

docker system prune -a -f --volumes

 

-f: Do not prompt for confirmation

 

--volumes: By default, the command doesn’t remove unused volumes to prevent losing important data. To remove all unused volumes, pass the --volumes option

 

-a: Remove all unused images not just dangling ones

Remove docker containers

docker rm <conrtainerID | container name>

 

docker container rm [OPTIONS] CONTAINER [CONTAINER...]

 

docker container rm zen_haslett

 

 

Docker containers are not automatically removed when you stop them unless you start the container using the --rm flag

 

Remove all stopped containers

docker container prune -f

 

 

To remove all stopped containers, invoke the docker container prune command

 

-f: Do not prompt for confirmation

 

Remove images

docker image prune

docker image prune -a

 

A dangling image is an image that is not tagged and is not used by any container. To remove dangling images use image prune

 

-a : To remove all images that are not referenced by any existing container, not just the dangling ones, use the prune command with the -a option

 

Remove docker volumes

docker volume rm -f VOLUME 

 

 

-f: Do not prompt for confirmation

 

Remove all unused local volumes

 

docker volume prune -f

 

 

-f: Do not prompt for confirmation

 

Remove one or more networks

 

docker network rm NETWORK [NETWORK...]
 

 

To remove a network, you must first disconnect any containers connected to it.

Remove all unused networks

 

docker network prune -f
 

 

Unused networks are those which are not referenced by any containers.

Execute Commands In Docker Container

Execute a command inside a running docker container

docker exec -it <container id> <command>

 

-it: allows to provide input to the container.

-i: input you type will be directed to standard IN of cli. Keep STDIN open even if not attached

-t:All the text you type will be user friendly formatted terminal. Allocate a pseudo-TT

 

Opens the shell prompt of the running container

docker exec -it <container id> sh

 

 

Get into the shell prompt inside the container while starting the container

docker run -it <image name> sh

docker run -it busybox sh

When you specify command while running the run , the defaut command specified by the image is not executed

Run tests on a running container

docker run exec -it <containerid> npm run test

 

 

DockerFile

Build the Dockerfile to generate an image

docker build .

 

"." represents the build context. Build context is a set of files and folders belonging to our project which we want to encapsulate in the container

*Note : When using a Dockerfile, the container will use the cached images instead of doing the entire build process again in subsequent builds.

In case a RUN command is changed, or a new RUN command is added in the dockerfile, this will cause docker to run the build process again for the changed/added step and all the steps after that line.

 

Even if the sequence of steps is changed, docker will execute the build process from the changed line and all the subsequent steps

 

If any changes are required in the steps, the change should be put down as far as possible.

 

Build the Dockerfile to generate an image with name of dockerfile specified

docker build -f <docker file name> .

 

Dockerfile.dev --> development version

Tagging an image during build

docker build -t <dockerid>/<repo/project name>:<version> .

 

 

Manually taking snapshot of a running docker container

docker commit -c "CMD '<default command of the new image>'" <container id>

 

docker run <image id>

docker commit -c "CMD 'redis-server'" <container id>

 

Create a new image from a container’s changes.

Commit a container’s file changes or settings into a new image

 

Multi Step Docker Build - Production Dockerfile

FROM node:alpine

WORKDIR '/app'

COPY package.json .

RUN npm install

COPY . .

RUN npm run build

 

FROM nginx

COPY --from=0 /app/build /usr/share/nginx/html

 

 

 

docker-compose commands

Run an image in a container

docker-compose up

 

 

Build the image and run in a container

docker-compose up --build

 

 

Run an image in a container in background

docker-compose up -d

 

 

Stop containers

docker-compose down

 

 

List the containers started by docker-compose

docker-compose ps

 

Should be run from the directory containing the docker-compose file

Mount Docker Volumes

Mount docker volumes for hot deployment

docker run -it -p <localhostport>:<containerport> -v <container directories not to be mapped to any folder> -v <localhost workdir  to be mapped>:<Container WORKDIR to be mapped> -e CHOKIDAR_USEPOLLING=true <imageid>

docker run -it --rm -p 5000:3000 -v /app/node_modules -v ${pwd}:/app -e CHOKIDAR_USEPOLLING=true princearoraaws/frontend

 

Docker Image Commands

Pull docker image

docker pull <image id>

 

 

Print out the steps used to create the nginx image

docker history < image id>

docker history nginx

 

Return low-level information on Docker objects

docker inspect [OPTIONS] NAME|ID [NAME|ID...]


 

Comments