Run Docker Image

If it does not exist locally, then at first download from docker hub to local. There are 2 ways:

docker run image-name additional-command

ex: docker run busybox echo Hello docker

OR

docker create image-name -> will return an container-id

ex: docker create busybox

68f87d2a0884ea9a2d34730c5667a2f72d2d8e911851367b68142918f642cd40

docker start -a CONTAINER_ID -> it will start the image default snapshot

ex: docker start -a 68f87d2a0884ea9a2d34730c5667a2f72d2d8e911851367b68142918f642cd40

Show container

To see currently running container:

docker ps

To see all runned container:

docker ps –all

Restart docker

check the docker id by docker ps –all, then start by

docker start -a CONTAINER_ID <- CONTAINER_ID from list

Stop Container

By default docker stop image after execution. But we can also stop running image by following command.

docker stop CONTAINER_ID -> may take maximum 10 seconds to stop (finally can docker kill if not able to stop withing 10 seconds)

docker kill CONTAINER_ID -> will stop instantly

Delete Docker Container

it will remove all container & related resources that run previously

docker system prune <- will provide options: [y/N]

Container Logs

to see to log of a container

docker logs CONTAINER_ID -> return the output of last run output

ex: docker logs dcd285f5c966

Executing Command Inside Containers

docker run image-name <- run image & generate container
docker exec -it CONTAINER_ID COMMAND

ex:
docker run redis
docker exec -it 91dfc9c190b1 redis-cli -> will provide option to write redis command

Start Shell

docker exec -it busybox sh <- to return from shell use “Ctrl + D” / exit

Run Isolated Container

start another terminal and execute

docker exec -it busybox sh

Categories: Docker

Leave a Reply

Your email address will not be published. Required fields are marked *