Docker basic commands everyone should know
### basic commmands ###
docker pull ubuntu //just pull
docker run image_name or id // pull and run
docker images
docker ps
docker ps -a
docker rm con_name
docker rm e01 er2 // few letters are enough
docker rmi image_name // containers should be stopped
docker stop container_id // or name
docker start container_id // date persists
docker run -d --name my_container_name ubuntu sleep 10 // -d background
docker exec container_id cat /etc/*release* //exec command on running container (detached)
docker run -it ubuntu bash // -it will login ; i will enable stdin
cat /etc/*release*
docker run jenkins
... some process
So you have to stop this container in another tab
so docker run -d jenkins // detached mode
Now attach to the terminal using
docker attach container_id
docker run -i myapp // so that it listens to the input or wait for the input
docker run -p 6000:80 mysql // check port 80 or what on docker file
docker run -p 6001:80 mysql
docker run -p 6002:80 mysql
Every docker container will have network ip
docker run -v /opt/data:/var/lib/mysqldata mysql // it will be mounted to docker container so that data is persisted
// This is volume mapping
docker run ubuntu:17.10 // 17.10 is a tag can be found in dockerhub
### notes ###
1. docker run -p 6060:8080 jenkins
docker inspect container_id //get network address
or docker container inspect container_id
get internal ip and type your_ip:8080
2. docker run -p 6060:8080 jenkins -v /mac_loc:/jenkins_home -u root
//persists data bind mount. check below
3. -e enviroment variables pass
-v mysql-db:/var/lib/mysql #named container #volume mount
Check in docker inspect container id
4.docker inspect container_id gives details
5.Mounting volume
docker run cont -v $(pwd):/usr/share/ngnix/html //bind mount ; check below
6. docker logs ; both stdout and stderr
docker logs -f container // continuous logs
###volume####
Named volume can be used to persist data in the host even if the container is removed.
docker volume inspect volume_name to check where it is persisting.
In order to store in your desired location, you can use bind mount.
docker volume create my_volume #this creates new volume and can be accessed by
docker volume ls # it was a named volume
docker volume inspect my_volume # to inspect it and check the location of the file
# They are created in /var/lib/docker/volumes
eg. docker run jenkins -v myvolume:/var/jenkins_home/location_in_dockerfile
Create another container and give myvolume. The data would have been persisted.
Another way called bind mount: docker run jenkins -v /Users/prajwram/mylocation:/var/jenkins_home/location_in_dockerfile
#This is bind mount
### docker build ###
docker build . //create image from docker file
Go the directory and type
docker build . -t prajwal_account/my_webapp
docker push prajwal_account/my_webapp
docker build .
docker build Dockerfile -t image_name // image_name is the image name docker file produces
docker build -f another_dockerfile -t image_name
docker history image_name // to check the size of the layers
### ubuntu ###
// do it manually first and then write this to dockerfile
apt-get update
apt-get install -y python // y required coz there is no input interaction
apt-get install -y python-pip
pip install flask
pip install flask-mysql
# Environment variables
docker run -e APP_COLOR=red container
and then inside docker container export it and then run the flask command
####dockerfile####
specify VOLUME /var/lib the data will be persistent
Note: Keep file called file in the current dir
FROM ubuntu
COPY file /opt/file
ENTRYPOINT ["cat"]
CMD ["/opt/file"]
Then save it
docker build -t cat_app
docker run cat_app
Note: ENTRYPOINT cat /opt/file also works
###dockercompose ###
Run using docker-compose up // run containers
docker-compose stop
docker-compose down // bring down and remove containers
image is must
Write inside service what you write in docker file
version: '3.1'
services:
service1: # any name you want
image: # image name
command: # command to be executed when container is up
environment:
volumes:
- .:/site
ports:
- '80:4000'
service2:
or
services:
web:
image: "prajwal/webapp"
ports:
- "80:5000"
database:
image: "mysql"
ports:
- "81:5000"
volumes:
- /opt/data:/var/lib/mysql
docker-compose up -d #up the containers
docker-compose down # stop the containers
docker-compose ps or
docker-compose top
### About entrypoint and command ###
Docker has a default entrypoint which is /bin/sh -c but does not have a default command.
When you run docker like this: docker run -i -t ubuntu bash the entrypoint is the default /bin/sh -c, the image is ubuntu and the command is bash.
The command is run via the entrypoint. i.e., the actual thing that gets executed is /bin/sh -c bash. This allowed Docker to implement RUN quickly by relying on the shell's parser.
Later on, people asked to be able to customize this, so ENTRYPOINT and --entrypoint were introduced.
Everything after ubuntu in the example above is the command and is passed to the entrypoint. When using the CMD instruction, it is exactly as if you were doing docker run -i -t ubuntu <cmd>. <cmd> will be the parameter of the entrypoint.
You will also get the same result if you instead type this command docker run -i -t ubuntu. You will still start a bash shell in the container because of the ubuntu Dockerfile specified a default CMD: CMD ["bash"]
As everything is passed to the entrypoint, you can have a very nice behavior from your images. @Jiri example is good, it shows how to use an image as a "binary". When using ["/bin/cat"] as entrypoint and then doing docker run img /etc/passwd, you get it, /etc/passwd is the command and is passed to the entrypoint so the end result execution is simply /bin/cat /etc/passwd.
Another example would be to have any cli as entrypoint. For instance, if you have a redis image, instead of running docker run redisimg redis -H something -u toto get key, you can simply have ENTRYPOINT ["redis", "-H", "something", "-u", "toto"] and then run like this for the same result: docker run redisimg get key.
FROM ubuntu:latest
MAINTAINER Rajdeep Dua "dua_rajdeep@yahoo.com"
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
$ docker run -d -p 5000:5000 flask-sample-one
http://containertutorials.com/docker-compose/flask-simple-app.html
Comments
Post a Comment