{"id":2448,"date":"2019-09-15T20:11:12","date_gmt":"2019-09-15T20:11:12","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=2448"},"modified":"2020-04-20T22:56:23","modified_gmt":"2020-04-20T22:56:23","slug":"docker-compose","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=2448","title":{"rendered":"5: Docker Compose"},"content":{"rendered":"<p><a href=\"\/docker-images\/\">&lt; 4 Docker Images<\/a> | <a href=\"http:\/\/wiki.thomasandsofia.com\/?p=2458\">6 Docker Registry &gt;<\/a><\/p>\n<p>47% Complete<\/p>\n<h1>26: Docker Compose<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/7894026#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/7894026#content<\/a><\/p>\n<p>Runs a YAML file with the instructions required to build a container or stack.<\/p>\n<pre>--link ContainerName:ContainerAlias<\/pre>\n<p>Creates a local alias in the container&#8217;s \/etc\/hosts file with the IP address of the container you are linking<\/p>\n<pre>172.17.0.3 ContainerAlias<\/pre>\n<h2>Docker Compose Versions<\/h2>\n<h3>Version 1<\/h3>\n<ul>\n<li>No networking controls<\/li>\n<li>No startup sequence or dependancies<\/li>\n<\/ul>\n<pre>redis:\r\n  image: redis\r\ndb:\r\n  image: postgres:9.4\r\nvote:\r\n  image: voting-app\r\n  ports:\r\n    -  5000:80\r\n  links:\r\n    - redis<\/pre>\n<h3>Version 2<\/h3>\n<ul>\n<li>Requires the version to be listed in the file.<\/li>\n<li>No need for links. Version 2 creates a bridged network that all containers are connected to.<\/li>\n<li>Includes &#8216;depends_on&#8217; property to show what containers are required before starting.<\/li>\n<\/ul>\n<pre>version: 2\r\nservices:\r\n redis:\r\n  image: redis\r\n db:\r\n  image: postgres:9.4\r\n vote:\r\n  image: voting-app\r\n  ports:\r\n   - 5000:80\r\n  depends_on:\r\n   - redis<\/pre>\n<h3>Version 3 (Current version)<\/h3>\n<ul>\n<li>Requires the version to be listed in the file.<\/li>\n<li>Supports Docker Swarm<\/li>\n<\/ul>\n<pre>version: 3\r\nservices:\r\n redis:\r\n  image: redis\r\n  networks:\r\n   - back-end db:\r\n db:\r\n  image: postgres:9.4\r\n  networks:\r\n   - back-end\r\n vote:\r\n  image: voting-app\r\n  networks:\r\n   - front-end\r\n   - backend\r\n  ports:\r\n   - 5000:80\r\n result:\r\n  image: result\r\n  networks:\r\n   - front-end\r\n   - back-end\r\nnetworks:\r\n front-end:\r\n back-end:<\/pre>\n<h1>28: Voting App<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/15829900#overview\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/15829900#overview<\/a><\/p>\n<p><a href=\"http:\/\/docker01.tas.local:5000\/\" target=\"_blank\" rel=\"noopener\">http:\/\/docker01.tas.local:5000\/<\/a><\/p>\n<ul>\n<li>5 containers, linking between containers, running in a specific order.<\/li>\n<li>Most containers built manually using pre-built Dockerfile(s)<\/li>\n<li>Vote on port 5000, result displayed on 5001<\/li>\n<li>Due to link constraints, containers had to be ran in order and some required special names for the links to work.<\/li>\n<\/ul>\n<h2>Download the code from Git<\/h2>\n<pre>git clone https:\/\/github.com\/dockersamples\/example-voting-app.git\r\ncd example-voting-ap<\/pre>\n<h2>Create and run the Submit Vote app<\/h2>\n<pre>cd vote\r\n# Dockerfile already exists, just build it.\r\ndocker build . -t voting-app\r\n# Run the container\r\ndocker run -p 5000:80 voting-app<\/pre>\n<p>Note:<\/p>\n<ul>\n<li>Container was not ran detached so you can see the logs<\/li>\n<\/ul>\n<h4>After the container is running access it via a web browser<\/h4>\n<ul>\n<li>http:\/\/docker01.tas.local:5000<\/li>\n<li>Click one of the links and watch the log. It will try to connect to redis, but will crash because the service is not yet running.<\/li>\n<\/ul>\n<h2>Run Redis<\/h2>\n<pre>Just need to pull this down, no mods necessary\r\ndocker run -d --name=redis redis<\/pre>\n<p>Note:<\/p>\n<ul>\n<li>The file still crashes. Since the containers have not been linked, they do not resolve<\/li>\n<\/ul>\n<pre>docker run -p 5000:80 --link redis:redis voting-app<\/pre>\n<ul>\n<li>Now, clicking links works without errors.<\/li>\n<\/ul>\n<h2>Run PostGres<\/h2>\n<p>This might seem out of order, but this database is required for the worker to run.<\/p>\n<pre>docker run -d --name=db postgres:9.4\r\n# run crashes - rerun attached to see errors\r\ndocker rm db\r\ndocker run --name=db= postgres:9.4\r\nError: Database is uninitialized and superuser password is not specified.\r\n       You must specify POSTGRES_PASSWORD for the superuser. Use\r\n       \"-e POSTGRES_PASSWORD=password\" to set it in \"docker run\".\r\n\r\n       You may also use POSTGRES_HOST_AUTH_METHOD=trust to allow all connections\r\n       without a password. This is *not* recommended. See PostgreSQL\r\n       documentation about \"trust\":\r\n       https:\/\/www.postgresql.org\/docs\/current\/auth-trust.html\r\n# Remove the Image\r\ndocker rm db\r\ndocker run --name=db -e POSTGRES_HOST_AUTH_METHOD=trust postgres:9.4\r\n<\/pre>\n<h2>Build and Deploy the Worker<\/h2>\n<p>12:00<\/p>\n<pre>cd ..\/worker\r\ndocker build . -t worker-app\r\n# guessing this will need to link to both dbs\r\ndocker run -d --link redis:redis --link db:db --name=worker worker-app<\/pre>\n<h2>Build and deploy the Resulting App<\/h2>\n<pre>cd ..\/result\r\ndocker build . -t result-app\r\ndocker run -d --link db:db -p 5001:80 --name result result-app<\/pre>\n<h2>Final results:<\/h2>\n<ul>\n<li>Voter Page: <a href=\"http:\/\/docker01.tas.local:5000\/\" target=\"_blank\" rel=\"noopener\">http:\/\/docker01.tas.local:5000\/<\/a><\/li>\n<li>Tally Page: <a href=\"http:\/\/docker01.tas.local:5001\/\" target=\"_blank\" rel=\"noopener\">http:\/\/docker01.tas.local:5001\/<\/a><\/li>\n<\/ul>\n<h1><\/h1>\n<h1>29: Docker Compose<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/15829906#overview\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/15829906#overview<\/a><\/p>\n<h2>Install Docker Compose<\/h2>\n<p>Docker compose not included by default, so needs to be installed.<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.docker.com\/compose\/\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.docker.com\/compose\/<\/a><\/li>\n<li><code>sudo curl -L \"https:\/\/github.com\/docker\/compose\/releases\/download\/1.24.1\/docker-compose-$(uname -s)-$(uname -m)\" -o \/usr\/local\/bin\/docker-compose<\/code><\/li>\n<li><code>sudo chmod +x \/usr\/local\/bin\/docker-compose<\/code><\/li>\n<\/ul>\n<h2>Stop and Remove all running containers<\/h2>\n<pre>docker stop $(docker ps -q)\r\ndocker rm $(docker ps -aq)<\/pre>\n<h2>Create the Docker Compose YAML file<\/h2>\n<p>nano docker-compose.yml<\/p>\n<pre>redis:\r\n  image: redis\r\ndb:\r\n  image: postgres:9.4\r\nvote:\r\n  image: voting-app\r\n  ports:\r\n    - 5000:80\r\n  links:\r\n    - redis\r\nworker:\r\n  image: worker-app\r\n  links:\r\n    - db\r\n    - redis\r\nresult:\r\n  image: result-app\r\n  ports:\r\n    - 5001:80\r\n  links:\r\n    - db<\/pre>\n<h2>Run the file<\/h2>\n<pre>docker-compose up<\/pre>\n<p>&nbsp;<\/p>\n<h1>30: 31: Pratice<\/h1>\n","protected":false},"excerpt":{"rendered":"<p>&lt; 4 Docker Images | 6 Docker Registry &gt; 47% Complete 26: Docker Compose https:\/\/www.udemy.com\/learn-docker\/learn\/lecture\/7894026#content Runs a YAML file with the instructions required to build a container or stack. &#8211;link ContainerName:ContainerAlias Creates a local alias in the container&#8217;s \/etc\/hosts file with the IP address of the container you are linking 172.17.0.3 ContainerAlias Docker Compose Versions ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=2448\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"class_list":["post-2448","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/2448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2448"}],"version-history":[{"count":15,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/2448\/revisions"}],"predecessor-version":[{"id":2680,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/2448\/revisions\/2680"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}