1. Create and Run Docker container from an image

$docker run <image_name>

docker run = docker create + docker start

Step 1:

$docker create hello-world

Step 2:

$docker start -a <container-id-from-step1>

-a : To attach to the container and show output in our terminal

2. Override command

$docker run <image_name> <command>

3. List of docker containers running

$docker ps

$docker ps --all

4. Restart stopped containers

$docker start <container-id>

5. Remove stopped containers

$docker system prune

6. Get Logs

$docker logs <container-id>

7. Stop a container

$docker stop <container-id>

8. Kill a container

$docker kill <container-id>

Note: Get <container-id> from docker ps command

9. Execute commands in a running container

$docker exec -it <container-id> <command>

10. -it flag

This flag is used to get input for docker container and pretty print output to the terminal

11. SH command in the docker container

$docker exec -it <container-id> sh

12. Creating Docker Images - Dockerfile(Stores the configuration for the container)

Step 1: Specify a base images
Step 2: Run some commands to install additional programs
Step 3: Specify a command to run on startup

13. Dockerfile

FROM alpine
RUN apk add —update redis
CMD [“redis-server”]

14. Tagging Image

$docker build -t <docker-id/<project-name>:<latest> .