docker-compose working but docker run not - docker

I have a docker-compose with just one image. This is the docker-compose.yml definition:
services:
myNodeApp:
image: "1234567890.dkr.ecr.us-west-1.amazonaws.com/myNodeApp:latest"
container_name: 'myNodeApp'
volumes:
- data:/root/data
But I want to move it to docker run as I am using just one container. Executing a docker run command as the following:
docker run 1234567890.dkr.ecr.us-west-1.amazonaws.com/myNodeApp:latest --name myNodeApp -v "data:/root/data"
But I get this message 1.12.4. However, executing docker-compose up starts the application and shows the log by output.
What is the difference? What is the equivalent of docker-compose up with docker? What am I doing differently?

I think you are looking for this?
docker run -it --name myNodeApp -v "data:/root/data"
1234567890.dkr.ecr.us-west-1.amazonaws.com/myNodeApp:latest
Or maybe this command would help you, because it will build a local image associated with the config in your docker-compose.yml .
docker-compose build
docker images

Related

How to find volume files from host while inside docker container?

In a docker-compose.yml file I have defined the following service:
php:
container_name: php
build:
context: ./container/php
dockerfile: Dockerfile
networks:
- saasnet
volumes:
- ./services:/var/www/html
- ./logs/php:/usr/local/etc/php-fpm.d/zz-log.conf
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
It all builds fine, and another service (nginx) using the same volume mapping, - ./services:/var/www/html finds php as expected, so it all works in the browser. So far, so good.
But now I want to go into the container because I want to run composer install from a certain directory inside the container. So I go into the container using:
docker run -it php bash
And I find myself in the container at /var/www/html, where I expect to be able to navigate as if I were on my host machine in ./services directory, but ls at this point inside the container shows no files at all.
What am I missing or not understanding about how this works?
Your problem is that your are not specifying the volume on your run command - docker run is not aware of your docker-compose.yml. If you want to run it with all your options as specifiend in it, you need to either use docker-compose run, or pass all options to docker run:
docker-compose run php bash
docker run -it -e B_PORT=3306 -e DB_HOST=database -v ./services:/var/www/html -v ./logs/php:/usr/local/etc/php-fpm.d/zz-log.conf php bash

How to make a docker-compose file from command lline

I run my container by five Docker commands as follows:
docker run --privileged -d -v /root/docker/data:/var/lib/mysql -p 8888:80 testimg:2 init
docker ps ---> to get container ID
docker exec -it container_id bash
docker exec container_id systemctl start mariadb
docker exec container_id systemctl start httpd
I was trying to do these steps by docker-compose but failed.
Can somebody make a docker-compose.yml or Dockerfile to get same result for me?
You're not going to be be able to do this with just a docker-compose.yml, because a compose file doesn't have any mechanism similar to docker exec. Additionally, running systemd (or really any process manager) inside a container is an anti-pattern. It can complicate the management and scaling of your containers, and in most cases doesn't provide you with any benefits.
Why don't you just have two images:
One that starts mariadb
One that starts Apache httpd
That might look something like:
version: "3"
services:
web:
image: httpd
ports:
- "8888:80"
db:
image: mariadb
volumes:
- "/root/docker/data:/var/lib/mysql"
You would probably need a custom image for the web server containing whatever application you're running, but you can definitely use the official mariadb image for your database.

Speeding up docker on Mac with docker-sync

I am trying to speed up a build on Docker for Mac using docker-sync. It takes about 20 minutes to build on Ubuntu, but upwards of 2 hours on a Mac.
I just wanted to go through the workflow to see if it makes sense.
command after the sync has started? Or should all the information in the docker run command above be put into the docker-sync yml files and then I just start the first two docker-sync commands?
Would love some clarity on this. Thanks!
I’ve got a docker run command to start docker that looks like this :
sudo docker run --privileged --mac-address xx:xx:xx:xx:xx:xx -h=“0000000000” -ti --user root --rm -v ~/local/path/to/files/:/path/to/files/on/docker/env
In order to get docker-sync going, I have made a docker-sync.yml file, a docker-compose.yml and a docker-compose-dev.yml, described below.
I had a few questions.
Do I need to run
docker-sync start
and then
docker-compose -f docker-compose.yml -f docker-compose-dev.yml up -d
and then run my
sudo docker run --privileged --mac-address xx:xx:xx:xx:xx:xx -h=“0000000000” -ti --user root --rm -v ~/local/path/to/files/:/path/to/files/on/docker/env
version: “2”
syncs:
simplest-sync: #tip: add -sync and you keep consistent names as a convention
src: ‘./[unique name of folder containing all source code on local Mac machine]’
my docker-compose.yml looks like this:
version: “2”
services:
app-simplest:
image: alpine
command: [‘watch’, ‘-n3’, ‘cat /var/www/index.html’]
my docker-compose-dev.yml looks like this :
version: “2”
services:
app-simplest:
volumes:
- /path/to/local/Sourcefiles/on/Mac:/path/to/files/on/docker/env
volumes:
simplest-sync:
external: true
I want to be able to eventually enter my docker container and execute the build in the /path/to/files/on/docker/env. It takes a very long time now, and I wish to speed it up. In its current configuration, there is no change to the speed using docker-sync.

Executing docker run command from config file

I have several arguments in my docker run command like
docker run --rm -v /apps/hastebin/data:/app/data --name hastebin -d -p 7777:7777 -e STORAGE_TYPE=file rlister/hastebin
Can I put all the arguments of this in a default/config file so that I dont have to mention it explicitly in the run command?
You can try docker compose
With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration
In your case docker-compose.yml file will looks like
version: '2'
services:
hastebin:
image: rlister/hastebin
ports:
- "7777:7777"
volumes:
- /apps/hastebin/data:/app/data
environment:
- STORAGE_TYPE=file
And you can run service by command docker-compose up

container exit with code 0 while using docker compose file

I have a dockerfile to install httpd. When i run this dockerfile using the command
docker run -dit /bin/bash,
the container is started and it is running in the background. when i perform docker ps i could see the container running.
I have created a docker-compose.yml file as below,
version: '2'
services:
web:
build:
context: ./web
dockerfile: Dockerfile-apache
image: web:1.0
container_name: web
ports:
- "80:80"
command: service httpd start
i have build this compose file using the
docker-compose build.
Once after that i started the containers using
docker-compose up -d.
The containers are getting exited. i am not sure how to make the containers run at background.
Also i want to make the services running inside the container. For example i need to run the command like service httpd start inside the container and how to do it ?
This is because a Docker container only lives as long as its command runs.
Your command service httpd start will start httpd in the background and then exit. This will terminate httpd and the container.
You will have to run the httpd process directly and in the foreground, see the official image's start script:
httpd -DFOREGROUND
You can't run docker with -dit options together. -d means to run it in background mode and -ti means an interaction with terminal. So, have to run with -d OR with -ti and not both

Resources