Docker-compose up does not start a container - docker

Dockerfile:
FROM shawnzhu/ruby-nodejs:0.12.7
RUN \
apt-get install git \
&& npm install -g bower gulp grunt \
gem install sass
RUN useradd -ms /bin/bash devel
# Deal with ssh
COPY ssh_keys/id_rsa /devel/.ssh/id_rsa
COPY ssh_keys/id_rsa.pub /devel/.ssh/id_rsa.pub
RUN echo "IdentityFile /devel/.ssh/id_rsa" > /devel/.ssh/config
# set root password
RUN echo 'root:password' | chpasswd
# Add gitconfig
COPY .gitconfig /devel/.gitconfig
USER devel
WORKDIR /var/www/
EXPOSE 80
docker-compose.yml file:
nodejs:
build: .
ports:
- "8001:80"
- "3000:3000"
volumes:
- ~/Web/docker/nodejs/www:/var/www
Commands:
$ docker-compose build nodejs
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
nodejs_nodejs latest aece5fb27134 2 minutes ago 596.5 MB
shawnzhu/ruby-nodejs 0.12.7 bbd5b568b88f 5 months ago 547.5 MB
$ docker-compose up -d nodejs
Creating nodejs_nodejs_1
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c24c6d0e756b nodejs_nodejs "/bin/bash" About a minute ago Exited (0) About a minute ago nodejs_nodejs_1
As you can see the docker-compose up -d should have created a container and run it on the background, but it didn't. Instead it exited with code 0.

If your Dockerfile doesn't do anything (for example a webserver to listen on port 80), it will be discarded as soon as it finishes running the instructions. Because Docker containers should be "ephemeral".
If you just want to start a container and interact with it via terminal, don't use docker-compose up -d, use the following instead:
docker run -it --entrypoint=/bin/bash [your_image_id]
This will start your container and run /bin/bash, the -it helps you keep the terminal session to interact with the container. When you are done doing your works, press Ctrl-D to exit.

I had a similar problem with SQL Server 2017 container exiting soon after it was created. The process running inside the container should be long running, otherwise Docker will exit the container. In the docker-compose scenario I implemented the tty:true approach which is documented here https://www.handsonarchitect.com/2018/01/docker-compose-tip-how-to-avoid-sql.html

Related

How get syncthing docker container to autostart on reboot with correct settings

I'm quite new to docker.
I followed the instructions here to install and run syncthing in a docker container.
That worked great, but when I restarted the PC it auto-started the syncthing docker container, but without the extra settings in the .sh file (no volumes, no other settings I think) - it was like it was running the container with only the default settings - wouldn't sync with any other devices, nothing other than the default share, etc.
When I tried to run syncthing from the syncthing_run.sh script, it complained that there was already a docker container running with the name syncthing and I needed to delete or rename the container in order to run with that name.
I was able to get it running again with all the settings by doing sudo docker stop syncthing to stop the already-running container, then removing the --name line from the script:
IDu=$(id -u $(logname)) # Saves the logged in user id in the IDu variable
IDg=$(id -g $(logname)) # Saves the logged in user group in the IDg variable
docker run -d \
--name syncthing \ # <------------- removed this line
--hostname=syncthing-redacted \
--network=host \
-v $PWD/st-sync/:/var/syncthing/ \
-v $PWD/data/:/var/syncthing/data/ \
-v /redacted/:/var/syncthing/redacted/ \
-e TZ="Australia/Sydney" \
-e PUID=$IDu \
-e PGID=$IDg \
--restart=unless-stopped \
syncthing/syncthing:latest
then running sudo ~/docker/syncthing/syncthing_run.sh.
It seems to be running fine now, but I think it has created a new container? When I run sudo docker container ls -a | grep syncthing, I get:
88e447e1cd78 syncthing/syncthing:latest "/bin/entrypoint.sh …" 27 minutes ago Up 27 minutes (healthy)
7dd04d0701a7 syncthing/syncthing:latest "/bin/entrypoint.sh …" 12 days ago Exited (0) 34 minutes ago
How can I fix everything so the container will auto-run on startup, but with the correct settings from the .sh script, and the correct name of syncthing??
Also, how can I remove the duplicate container, and is it safe for me to do so without losing any data?
I've discovered that docker run and docker start are different. docker run creates and then starts a new container.
What I should have done after the reboot is sudo docker start syncthing.
Running the script again created a new container. I had to stop the running container with sudo docker stop 88e447e1cd78 and then delete it with sudo docker rm 88e447e1cd78.
I think I could have just used sudo docker start syncthing after that point, but I also deleted the old container and then ran the script again to create a new container after reenabling the --name line.

I can't get access to an exposed port in docker

I'm using Ubuntu 20.04 and running Python 3.8. Here is my dockerfile:
FROM python:3.8
WORKDIR /usr/src/flog/
COPY requirements/ requirements/
RUN pip install -r requirements/dev.txt
RUN pip install gunicorn
COPY flog/ flog/
COPY migrations/ migrations/
COPY wsgi.py ./
COPY docker_boot.sh ./
RUN chmod +x docker_boot.sh
ENV FLASK_APP wsgi.py
EXPOSE 5000
ENTRYPOINT ["./docker_boot.sh"]
and my docker_boot.sh
#! /bin/sh
flask deploy
flask create-admin
flask forge
exec gunicorn -b 0.0.0.0:5000 --access-logfile - --error-logfile - wsgi:app
I ran docker run flog -d -p 5000:5000 in my terminal. And I couldn't get my app working by typing localhost:5000 but it worked quite well when I typed 172.17.0.2:5000 (the docker machine's ip address). But I want the app to run on localhost:5000.
I'm sure there is nothing wrong with the requirements/dev.txt and the code because it works well when I run flask run directly in my terminal.
Edit on 2021.3.16:
Add docker ps information when docker run flog -d -p 5000:5000 is running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff048e904183 flog "./docker_boot.sh -d…" 8 seconds ago Up 6 seconds 5000/tcp inspiring_kalam
It is strange that there's no mapping of the hosts. I'm sure the firewall is off.
Can anyone help me? Thanks.
Use docker run -d -p 0.0.0.0:5000:5000 flog.
The arguments and the flags that are after the image name are passed as arguments to the entrypoint of the container created from that image.
Run docker ps and you need to see something like
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
565a97468fc7 flog "docker_boot.sh" 1 minute ago Up 1 minutes 0.0.0.0:5000->5000/tcp xxxxxxxx_xxxxxxx

Container in docker is getting started but couldnt be found in "docker ps" command

I have created a 2 containers in docker. However, one of them is visible and other is not.
Context:
I have created 1 container by downloading the docker jenkins image file and that is up and running and can be seen using docker ps command.
Then, I have tried to create an Image file to be consumed by the second container.
The script I have used in VI to create image file:
FROM centos
RUN yum -y install openssh-server
RUN yum install -y passwd
RUN useradd remote_user && \
echo "1234" | passwd remote_user --stdin && \
mkdir /home/remote_user/.ssh && \
chmod 700 /home/remote_user/.ssh
COPY remote-key.pub /home/remote_user/.ssh/authorized_keys
RUN chown remote_user:remote_user -R /home/remote_user/.ssh/ && \
chmod 600 /home/remote_user/.ssh/authorized_keys
CMD /usr/sbin/sshd -D
The script ran successfully as "docker-compose build" has successfully build the image from the script.
Once it was successfully built, I tried to start the it using:
[jenkins#localhost jenkins-data]$ docker-compose up -d
jenkins is up-to-date
Starting remote-host ... done
Post this, when i am doing :
[jenkins#localhost jenkins-data]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c1ee0507091 jenkins/jenkins "/sbin/tini -- /usr/…" 5 days ago Up 5 minutes 0.0.0.0:8080->8080/tcp, 50000/tcp jenkins
Its only showing me one container running while the remote-host container is not visible.
Any way to ensure if the remote-host container is actually running or is there any issue?
New to docker and jenkins, any lead is highly appreciated. thank you.
docker ps only shows running containers.
Using docker ps -a you see both running and stopped containers.
See Docker documentation about ps.
Probably the remote-host container is not running any more?
Container stopped because of the main process which launched by the CMD command detached and become daemon
the main process should be attached to the terminal so you have to remove -D from CMD command CMD /usr/sbin/sshd -D or you can follow this approach
run sshd in detached mode and use while sleep to keep the container
running

docker, mariadb doesn't start at "init", based in debian:stable

i am trying write a Dockerfile like that
FROM debian:stable
RUN apt-get update
RUN apt-get install -y mariadb-server
EXPOSE 3306
CMD ["mysqld"]
I create the image with
docker build -t debian1 .
And i create the container with
docker run -d --name my_container_debian -i -t debian1
20 seconds after, docker ps -a tells that container is exited. Why? I want the container is up and mariadb running. Thanks. Sorry for the question.
mysqld alone would exit too soon.
If you look at a MySQL server Dockerfile, you will note its ENTRYPOINT is a script docker-entrypoint.sh which will exec mysqld in foreground.
exec "$#"

Simple Dockerfile no work

This is my Dockerfile:
FROM debian:stable
MAINTAINER xxxx <xxxx#xxxx.com>
RUN apt-get update && apt-get upgrade -y
CMD ["/bin/bash"]
Then, I run in the directory of Dockerfile:
docker build -t testimage .
Finally:
docker run -d testimage
The container no start:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c4fe93e2e225 test "/bin/bash" 17 minutes ago Exited (0) 9 minutes ago gloomy_ritchie
You are trying to run a detached container (-d), but you are also attempting to launch an interactive shell (/bin/bash). Because bash requires an interactive terminal, it exits immediately, hence your container exits.
If you just want to run an interactive shell in your container, get rid of the -d:
docker run -it testimage
The -it flags set up the container for interactive use; see the man page for docker-run for more information.
A detached container is most often used to run a persistent service (like a database, or a web server), although you can run anything as long as it doesn't expect to be attached to an active terminal.

Resources