Just execute the commands and you will get taste of Docker.
We will use docker images present on docker hub.
Later we will create a simple docker image and play with it.
We will use hello-world
image from docker hub to try out basic commands.
Commands covered:
pull, push, ls, ps, run, logs, start, stop, restart, rm
docker pull hello-world
docker pull will always pull ‘latest’ tag if not specified.
You will need docker hub account for this (Can skip for now).
docker push hello-world
docker images
docker image ls
We will use hello-world
image to try out docker commands.
docker run hello-world
Give name of your choice to the container.
docker run --name mycontainer hello-world
docker ps
docker container ls
docker ps -a
docker container ls -a
docker logs mycontainer
You can stream the logs using -f
or --follow
.
docker logs -f mycontainer
docker logs --follow mycontainer
docker start mycontainer
docker stop mycontainer
docker restart mycontainer
Container must be stopped before removing.
docker rm mycontainer
docker rm -f mycontainer
docker rm --force mycontainer
The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.
We have multiple options available with docker run
which can be used to run docker container.
docker run -it --name mytomcat --rm -p 8888:8080 tomcat:9.0
option | Description |
---|---|
--interactive , -i |
Keep STDIN open even if not attached |
--tty , -t |
Allocate a pseudo-TTY |
--rm |
Automatically remove the container when it exits |
--publish , -p |
Publish a container’s port(s) to the host |
Visit http://localhost:8888
You should get 404, this means that your container is up and running
You can try out commands in above section on this container.
Press Ctrl+c
to exit container.
Create a directory and use it as docker context
mkdir docker-learn
cd docker-learn
Create a file named Dockerfile
.
Add below lines to the file.
FROM alpine
ENTRYPOINT ["sh","run.sh"]
Create a file named run.sh
.
Add below lines to the file.
#!/bin/sh
echo "Yay! I have created a docker image and started container using it"
Execute below command to create docker image.
docker build -t myfirstimage .
This will create an image named myfirstimage
.
We will run the docker image created above.
Execute below command to run the image.
docker run --name myfirstcontainer myfirstimage
You should get output as:
Yay! I have created a docker image and started container using it
Next: Docker Cheatsheet