DevOps ~ Docker Commands - rohit120582sharma/Documentation GitHub Wiki

Image based command

Building an image from a Docker file

# -t: to tag the image name
# -f: to configure the file path
$ Docker build -t myapp -f ./Dockerfile .

Listing all images that are locally stored

$ docker images

Removing named image

$ docker rmi <image-name>

Removing unnamed images

$ docker rmi $(docker images -q -f dangling=true)


Container based command

Creating and starting a container from an image

# --name: to tag the container name
# -it: to make an interaction with a container
# -d: to detach a container on start
# -p: to publish the host IP and the host port to the container port
# -v: to define and share the volume across containers
$ docker run <image-name>
$ docker run --name <container-name> -p 3000:3000 <image-name>
$ docker run -it <image-name> sh

Listing running containers/processes

$ docker ps

Listing running and stopped containers

$ docker ps -a
$ docker ps --all

Container lifecycle. These two commands are equivalent to 'docker run' command

$ docker create <image-name> # It returns a newly created <container-id>
$ docker start -a <container-id>

Retrieving log outputs

$ docker logs <container-id>

Inspect a container

$ docker inspect <container-id>

Stop container

$ docker stop <container-id>
$ docker kill <container-id>

Removing stopped container

$ docker system prune
$ docker rm <container-id>

Execute an additional command in a running container

# -it allows us to provide input to the container
$ docker exec -it <container-id> <command>

Getting a command prompt in a container

$ docker exec -it <container-id> sh

Creating image out of a container. It will return a newly created image-id

# $ docker commit -c <startup-command> <container-id>
$ docker commit -c 'CMD ["redis-server"]' <container-id>
⚠️ **GitHub.com Fallback** ⚠️