Docker

A few useful commands when using Docker

1. Pull a Docker image from the Docker Hub repository

docker pull ubuntu:18.04

2. Run a Docker image in interactive mode

docker run -it ubuntu:18.04

3. Stop a running Docker container

docker stop <container-id>

4. Restart a Docker container (assuming it exists) in interactive mode

docker restart <container-id>
docker exec -it <container-id> bash

5. Remove a Docker image

docker rmi ubuntu:18.04

6. Remove a Docker container

docker rm <container-id>

7. Remove all Docker stopped containers

docker container prune

8. List all Docker images

docker images

9. List all Docker containers

docker ps -a

10. How to build a Docker image from a Docker container

# Modify a new container
docker run --name hw_container ubuntu:latest
   touch /HelloWorld

# Commit the changes you made in that container
# to a new image
docker commit hw_container hw_image

# Remove the changed container
docker rm -vf hw_container

# Test the new image docker run --rm hw_image
# ls -l /HelloWorld
# Outputs:
# -rw-r--r-- 1 root root 0 Apr 15 22:06
# /HelloWorld