To build a docker image from Dockerfile
docker build
To pull a docker image
docker pull
To run a docker image
docker run
Find docker version
docker version
docker -v
Find docker installation details and how many containers currently running on docker host
docker info
Execute a command in docker container
docker run docker/whalesay cowsay Hello World
Login to a docker container with interactive shell
docker run -it ubuntu bash
Login to a docker container with interactive shell as root user
docker run -u 0 -it ubuntu bash
List all the images from local docker repository
docker images
Search the image name called debian from the docker hub image repository
docker search debian
Pull the debian image from docker hub image repository
docker pull debian
List details about the debian image, like ID, Tag, Creation Date, Container configuration
docker inspect debian
Will show history of each layer that was added to the debain docker image
docker history debian
Save nginx image in tar format)
docker save nginx -o nginx.tar
docker load -i ./nginx.tar
Delete a docker image
Search for the image alpine
docker images alpine
Delete the alpine image
docker rmi alpine
Delete all docker images
docker rmi $(docker images -q)
Indentify dangling docker images
docker images -f "dangling=true"
Delete all dangling images
docker rmi $(docker images -f "dangling=true" -q)
How to tag an image during build
docker build myimage -t"myfirstimage:latest"
docker images myfirstimage
List all containers running and terminated
docker ps -a
Run a docker image as a container instance
docker run fedora /bin/echo 'Hello World'
Run a docker container with interactive shell
docker run -it fedora /bin/bash
Detach or a container and run in background
docker run -d ubuntu /bin/sh -c "while true; do echo current date and time is: $(date); sleep 10; done"
docker ps
docker logs <docker hash id>
Login to a docker container instance
docker exec -it <container hash id> /bin/bash
Steps to create a Docker Image
Step 1:
Create Dockerfile
cat myimage/Dockerfile
FROM ubuntu
RUN echo "my first image" >/tmp/first.txt
Step 2:
Build the image from Dockerfile
docker build myimage
Step 3:
Check if the image is created
docker images | grep <docker image hashid from step2>
Step 4:
Create a container instance
docker run -it <docker image hashid from step2>
How to stop and remove a container instance
Step 1:
First stop the container instance
docker stop <container hash id>
Step 2:
Remove the container
docker rm <container hash id>
How do I remove all containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker ps -a
Steps to create a NGINX Docker Instance.
Step 1:
cat DockerFile
FROM: nginx:latest
MAINTENER Naresh Padiyar
ADD ./index.html /usr/share/nginx/html/index.html
EXPOSE 80
cat index.html
<h1> welcome to Dockerizing app !<h1>
Step 2:
docker build .
Step 3:
docker images
Step 4:
docker run -p 80:80 -d <docker hash id from step 3>
Step 5:
curl localhost:80
How do I run a C Program
docker pull gcc
docker pull ubuntu:latest
docker run -it ubuntu /bin/bash
apt-get update
aget-get install gcc
cat >hello.c
int main() { printf("hello world\n”); }
gcc -w hello.c
./a.out
How do I run a Java program
Step 1:
cat Dockerfile
FROM java:latest
COPY . /usr/src/
WORKDIR /usr/src/
RUN javac hello.java
CMD ["java","hello"]
cat hello.java
call hello {
public static void main(String []args) {
System.out.println("Hello World");
}
}
Step 2:
docker build . -t"myjavaapp:latest"
Step 3:
docker images
Step 4:
docker run myjavaapp
How to push images to docker hub ?
docker tag myjavaapp nareshpadiyar/myfirstjavaprog:latest
docker push nareshpadiyar/myfirstjavaprog:latest
How to pull images from docker hub and run a container instance ?
docker pull nareshpadiyar/myfirstjavaprog:latest
docker images
docker run nareshpadiyar/myfirstjavaprog