First install Docker CE, see http://magnus-k-karlsson.blogspot.com/2019/08/install-docker-community-edition-ce-on.html.
To run docker containers, you can either run them in the foreground or the background.
Foreground:
$ docker run -i -t busybox:1.31.0
Unable to find image 'busybox:1.31.0' locally
1.31.0: Pulling from library/busybox
ee153a04d683: Pull complete
Digest: sha256:9f1003c480699be56815db0f8146ad2e22efea85129b5b5983d0e0fb52d9ab70
Status: Downloaded newer image for busybox:1.31.0
/ # ls
bin dev etc home proc root sys tmp usr var
-i, run interactively
-t, connect with terminal, i.e. with same terminal as open.
To exit simply, type exit, as exiting from a normal terminal window.
Background
$ docker run -d -p 8080:80 nginx:1.17.2
Unable to find image 'nginx:1.17.2' locally
1.17.2: Pulling from library/nginx
f5d23c7fed46: Pull complete
918b255d86e5: Pull complete
8c0120a6f561: Pull complete
Digest: sha256:eb3320e2f9ca409b7c0aa71aea3cf7ce7d018f03a372564dbdb023646958770b
Status: Downloaded newer image for nginx:1.17.2
aa32c45c0cf9ca9683024f07d83378ddc73e9c6520fff052a8d104599918731a
-d, run container as daemon
-p, port mapping, part local port 8080 to container port 80
Now lets test with curl
$ curl 127.0.0.1:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
To see current running containers, use docker ps
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa32c45c0cf9 nginx:1.17.2 "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp thirsty_proskuriakova
To stop a docker container use docker stop aa32c45c0cf9 or docker kill aa32c45c0cf9, to stop it not gracefully.
All docker containers get automatically an ID, to give it a more friendly name use --name mynginx, but since containers ID needs to be unique, you cannot stop the container and rerun again with same name. To avoid that you can remove the container "history" with --rm
$ docker run -d -p 8080:80 --name mynginx --rm nginx:1.17.2
1f5c642843c12bf3b8f0e91c92e603e409e8f32cfa44b957457587618e14693f
Now if you stop nginx and lets all process, you will not see nginx.
$ docker stop mynginx
mynginx
$ docker ps -a
When you need to debug, these commands are useful:
$ docker logs <CONTAINERID|NAME>
$ docker inspect <CONTAINERID|NAME>
And to connect to docker container
$ docker exec -it <CONTAINERID|NAME> bash
More complex containers take input parameters - environment variable. Lets open up dockerhub and the official mysql repository - https://hub.docker.com/_/mysql. When you scroll down that page you see several environment variables. Lets test them. NOTE that MYSQL_ROOT_PASSWORD is mandatory.
$ docker run -d -p 3306:3306 --name mysql --rm -e MYSQL_ROOT_PASSWORD=redhat123 -e MYSQL_DATABASE=mydb -e MYSQL_USER=mydb_admin -e MYSQL_PASSWORD=redhat123 mysql:8.0.17
Now lets test to connect from the docker host.
$ mysql -u mydb_admin -h 127.0.0.1 -p mydb
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [mydb]>
No comments:
Post a Comment