Docker Compose Tip : How to avoid SQL Server 2017 container exiting when running using docker compose

Problem starting SQL 2017 container

While working on recent blog post on Integrating SQL Server 2017 Linux with ASP.Net Core using Docker I cam across a strange behavior. I was able to run SQL Server 2017 Linux container image successfully as demonstrated here. I was able to initialize the database using the command

docker run -it -p 1433:1433 \
--name sql2017 \
nileshgule/sqldb

This command runs the custom image in interactive mode based on the –i flag. We also pass the t flag to create a psudo tty terminal. The next step for me was to try the integration with Dotnet Core project. I started with updating the docker-compose file and other changes explained in the post on integration.

Before I added the sql2017 as the service inside docker-compose file, I ran the SQL Server 2017 container individually using the above command & verified that the Core MVC and Core Web API we able to communicate to the database inside container.

This seemed pretty easy at first. All I had to do was update docker-compose file with additional service for SQL Server. I did that change and fired the docker compose build command. The build command was executed successfully and 3 images were produced.

I fired up the docker compose up command to bring up all the services using the command

docker-compose -f docker-compose-build.yml up -d

I was expecting all the services to interact together. It was too good to pass at the first attempt. Instead what I got was a problem with starting the SQL 2017 container.

container exited

The container exited with code 0. The MVC and web API containers were created successfully. I thought it might be a problem with the detached mode which I had used with –d flag with docker-compose. So I tried to run the command in interactive mode by removing the flag as

docker-compose -f docker-compose-build.yml up

Even with this change I got the same result.

Root cause of the problem

Why does the container exits in docker-compose mode immediately? If I run the standalone container again everything was working as expected. So there had to be some reason specific to docker-compose and nothing to do with the custom image. After looking around for possible reason on the forums I realized what the problem was. Any guess?

In the docker-compose mode, docker tries to start all the container in detached mode by default. If there is no process running inside the container within certain duration, docker thinks the container has finished its work and exits it. In our case in order to initialize the TechTalksDB we are running the initializing script after waiting for few seconds (between 10 to 30 seconds). This is not a foreground process. It runs as a background process. Docker things the foreground process has finished and exits the container.

We need some way of keeping the container alive and ensure that docker does not exit the container. Docker-compose does not support –t as a command line parameter. In absence of command line parameter, we need some way of simulating the standalone behavior of tty psudo terminal. We can amend the sqk2017 service by adding tty: true option as shown below on line 13

Rebuild the images by running docker-compose build command after making this change and run the docker-compose up command as shown above. Now we have the sql2017 container working as expected.

container runs successfully

Conclusion

The complete source code used for this post is available on Github. If you have container which runs some process in the background, it might exit immediately after starting. Use tty:true to keep the service running. It can be quite irritating when things work in isolation using docker run commands but fail in docker compose mode. That is why it is important to understand the differences between different docker commands and how they operate. There might be minor differences between commands in standalone mode and in compose mode. I tend to use the online docker docs as well as the help with the –help command on docker CLI commands. The best way to fix these issue are by hit and trial mode. I hope you found this little trick useful. Until next time code with passion and strive for excellence.

Share:
spacer

1 comment:

  1. Thanks for your post, I followed along but I am getting errors about "An unhandled exception occurred while processing the request" sqlException....
    Here is my code https://github.com/link78/aspnet3.1-containers.git

    ReplyDelete