If you are a beginners in docker, then you have to Below are the basic commands for docker
For pull the docker image
# docker pull image:tag
Note: If you not mentioned the tag, then it will automatically pick the “latest” tag.
so the command looks like below
# docker pull ubuntu:latest
Show docker images
# docker images
This will show you all images that you have pulled in your local repository.
Build an image from DockerFile
# docker build -t <newimagename:tag> .
Run a container from docker image
# docker run --publish=80:80 --name=<anyname> <docker-image-name:tag>
Below are the command explain:
- –publish – You can see two port are mentioned in this arguments by separate with “:”. First one indicates that what port will use in your host pc and second port indicates what port you want to use in your docker container. You can expose any available port here.
- –name – Put your docker container name here.
- You have to mentioned the docker image name with the tag here.
So the commands look like below
# docker run --publish=80:80 --name=test-server ubuntu:latest
Run a container from docker image (Detach Mode)
You can run container in attached mode (in the foreground) or in detached mode (in the background). By default, Docker runs the container in attached mode. In the attached mode, Docker can start the process in the container and attach the console to the process’s standard input, standard output, and standard error
To run the container in detach mode you need to pass -d argument in the command line.
# docker run -d ubuntu:latest
Run a container from docker image with privilege
By default, Docker containers are “unprivileged” to run a Docker daemon inside a Docker container. This is because by default a container is not allowed to access any devices, but a “privileged” container is given access to all devices
# docker run --publish=80:80 --privileged --name=test-server ubuntu:latest
When you give –privileged, Docker will enable access to all devices on the host.
Login to the existing running container
# docker container exec -it <container-name/container-id> bash/sh
-i = keeps stdin open even if not attached
-t = allocates a tty
Change Default container commands
# docker container run -d <image-name> <commands>
Take image from a running container
# docker commit <containerID> <newimagename:tag>
This will take a image from a running container
Stop a running container
# docker stop <containerID>
Start a stopped container
# docker start <containerID>