--detach , -d Run container in background and print container ID
--name Assign a name to the container
--publish , -p Publish a container’s port(s) to the host
--volume , -v Bind mount a volume
--restart Restart policy to apply when a container exits
- Show the Docker version information
docker version
- Display system-wide information
docker info
- You can use the docker inspect command to find out low-level information about your container or image
docker inspect [Container or Image name /ID]
- Pull an image or a repository from a registry
docker pull ubuntu
- pull a docker image with old version
docker pull ubuntu:16.04
- List of all available images in local
docker images
- Launching a container with ‘docker run’ command
docker run [imagename|ImageId]
Create/run/start a docker container from image
docker run -d --name <container_Name> <image_name>:<image_version/tag>
d - run your container in back ground (detached)
Expose your application to host server
docker run -d -p <host_port>:<container_port> --name <container_Name> <image_name>:<Image_version/tag>
Example : docker run -d --name httpd_server -p 8080:80 httpd:2.2
- run a OS based container which interactive mode (nothing but login to container after it is up and running)
docker run -i -t --name <container_Name> <image_name>:<Image_version/tag>
i - interactive
t - Terminal
Example: docker run -i -t --name ubuntu_server ubuntu:latest
- List out running containers
docker ps
- List out all docker container (running, stpooed, terminated, etc...)
docker ps -a
- Stop a container
docker stop <container_id>
- start a container which is in stopped or exit state
docker start <container_id>
- Remove a container
docker rm <container_id>
- Kill a container by sending a SIGKILL to a running container
docker kill [CONTAINER]
- Remove the image
docker rmi [IMAGE]
- Docker Exec Bash and Docker SSH into Container You'll often need to interact with a container's shell to create a service or solve problems. You can use the docker exec command to create an interactive shell. Let's start a container from the ubuntu image with a bash shell:
docker run --name my_ubuntu -it ubuntu:latest bash
Suppose you want to Docker ssh into container 'my_ubuntu'. You can use the docker exec bash method:
docker exec -it my_ubuntu bash
Use docker exec to issue commands into your container. For example, you can run the ls command on your 'my_ubuntu' docker container directly from the command prompt:
docker exec -it my_ubuntu ls
-
The Dockerfile is a text file that (mostly) contains the instructions that you would execute on the command line to create an image.
-
You’ll use a Dockerfile to create your own custom Docker image.
-
Docker will build a Docker image automatically by reading these instructions from the Dockerfile.
Creating the Custom Docker image below is the process
-
you create the Dockerfile and define the steps that build up your images
-
you issue the docker build command which will build a Docker image from your Dockerfile
-
now you can use this image to start containers with the docker run command
Docker provides a set of standard instructions to be used in the Dockerfile, like FROM, COPY, RUN, ENV, EXPOSE, CMD just to name a few basic ones.
Step 1. create the DockerFile
vi Dockerfile
Step 2. Paste below instruction paste into Dockerfile
FROM ubuntu
RUN mkdir TEST
RUN apt-get update
RUN apt-get install tree
Step 3. docker build -t
docker build -t ubuntu_server .
. is represent that Dockerfile is present in same Directory .
Step 4. To create the container from Docker Image. docker run -d --name <image_name>:<Image_version/tag>
docker run -d --name ubuntu_container ubuntu_server
Create custom image for Nginx server .
Step 1. Create Index.html file
vi index.html
Step 2. Put Some content into the index.html
<html>
<body>
<h1> This is sample example of docker ngnix </h1>
</body>
</html>
Step 3: Create the DockerFile in same Directory
vi Dockerfile
Step 3: Paste below content into the Dockerfile and save (press esc btn then wq! end enter to save the file).
FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html // Copy the file from source location to Destination
RUN chmod +r /usr/share/nginx/html/index.html // Providing the permission to index.html
CMD ["nginx","-g","daemon off;"] // Nginx uses the daemon off directive to run in the foreground.