Docker
#yum install -y docker-io.x86_64
#docker --version
#/sbin/service docker start
#/sbin/chkconfig docker on 设置开机启动
docker images
docker search oraclelinux
docker ps
docker ps -all
This should show you the container that was launched a few minutes ago by you.
docker pull selenium/node-firefox
$ docker help
$ docker COMMAND --help
$ docker version
$ docker info
$ docker run -t -i busybox:2.0
$ docker run -t -i busybox
We have provided -i -t as the parameters to the run command and this means that it is interactive and attaches the tty input.
$ docker start -i ab0e37214358
Note the -i for going into interactive mode.
$ docker start ab0e37214358
$ docker attach ab0e37214358
We can attach to a running Container via the docker attach command.
Docker Commands Basic Tutorial:
https://rominirani.com/docker-tutorial-series-part-2-basic-commands-baaf70807fd3
Docker Commands Reference:
https://docs.docker.com/engine/reference/commandline/docker/#child-commands
$ docker run -t -i busybox
The run command does something interesting and this will help you understand the Docker architecture, which we have seen earlier. The run command does the following:
1. It checks if you already have a busybox image in your local repository.
2. If it does not find that (which will be the case first time), it will pull the image from the Docker hub. Pulling the image is similar to downloading it and it could take a while to do that depending on your internet connection.
3. Once it is pulled successfully, it is present in your local repository and hence it is then able to create a container based on this image.
4. We have provided -i -t as the parameters to the run command and this means that it is interactive and attaches the tty input.
Docker应用场景:
http://blog.51cto.com/lizhenliang/1978081
http://www.dockone.io/article/1282
https://confluence.oraclecorp.com/confluence/display/OSCDS/Docker+image+with+firefox+and+ATS+agent+for+Fusion
Images
- weblogic
- Database
- Selenium node
Docker Compose
https://www.cnblogs.com/neptunemoon/p/6512121.html
https://github.com/zhangpeihao/LearningDocker/blob/master/manuscript/04-WriteDockerfile.md
Common commands:
Fetch the image from the docker registry
$ docker image pull hello-world
Check local images
$ docker image ls
Run an image. If the specified image is not pulled yet, it will be automatically pulled.
$ docker container run hello-world
Shut down the the container right now.
$ docker container kill [containID]
Check the running container
$ docker container ls
Check all the containers including the closed ones.
$ docker container ls --all
Delete the container file to release disk space.
$ docker container rm [containerID]
-p is used to specify the port mapping. 8000 is local port. 3000 is the exposed port of the container.
/bin/bash is the first command after the container is up.
$ docker container run -p 8000:3000 -it koa-demo:0.0.1 /bin/bash
Exit from the container:
Ctrl+d or exit
Automatically delete the container file (using --rm) after stopped:
$ docker container run --rm -p 8000:3000 -it koa-demo /bin/bash
Start the existing container instead of creating a new one.
$ docker container start [containerID]
Stop the container. It may be stopped a little later.
$ bash container stop [containerID]
Check the container output:
$ docker container logs [containerID]
Go into a running container. It is useful when we didn't specify -it when start the container.
$ docker container exec -it [containerID] /bin/bash
Copy files from the container to local machine.
$ docker container cp [containID]:[/path/to/file] .
Dockerfile
- specify the working directory.
WORKDIR /app
- Run the npm install to install dependency under the /app directory.
RUN npm install --registry=https://registry.npm.taobao.org
-Expose container port
EXPOSE 3000
- Run a command (node demos/01.js) using CMD after the container is up. There is only one CMD in a Dockerfile.
CMD node demos/01.js
COPY
ENV
USER
ARG
For example, "&&" is used to split multiple commands, "\" is used to change line
RUN chmod -R 775 /u01 && \
chown -R ydu:dba /u01
RUN apt-get -qqy update \
&& apt-get -qqy --no-install-recommends install
Linux commands:
Install a package:
$apt-get install packagename
Upgrade all existing packages:
$apt-get upgrade
Search package in Ubuntu:
$apt-cache search tomcat
Create image file, -t for image file name, ":.0.0.1" is the tag. The . is used to specify the Dockerfile location.
$ docker image build -t koa-demo:0.0.1 .
$ docker login
$ docker image tag [imageName] [username]/[repository]:[tag]
For example,
$ docker image tag koa-demos:0.0.1 ruanyf/koa-demos:0.0.1
Publish the image:
$ docker image push [username]/[repository]:[tag]
$docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
The --name flag allows us to specify our own name for the created container.
-e is one way of passing an Environment Variable into a Docker container.
or using --env
$docker run --help
--name, Assign a name to the container
-d, run container in background
--env-file, Read environment variables from a file
-t, --tty, Allocate a pseudo-TTY
$docker volume create crv_mysql
$docker run \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
-e MYSQL_DATABASE=devdb \
-e MYSQL_USER=dbuser \
-e MYSQL_PASSWORD=dbpassword \
--mount type=volume,src=crv_mysql,dst=/var/lib/mysql \
-p 3306:3306 \
-d \
mysql:latest
$cat .env
MYSQL_ROOT_PASSWORD=my-secret-pw
MYSQL_DATABASE=devdb
MYSQL_USER=dbuser
MYSQL_PASSWORD=dbpassword
$docker run \
--env-file .env \
--mount type=volume,src=crv_mysql,dst=/var/lib/mysql \
-p 3306:3306 \
-d \
mysql:latest